//@dart=2.12
import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/bad_event/bad_event_model.dart';

class ChooseServiceGroupViewModel extends BaseViewModel{

  List groupList = [];

  List<dynamic> selectedList = [];

  ChooseServiceGroupViewModel(BuildContext? context,{List<dynamic> chooseList = const []}) : super(context){
    selectedList.addAll(chooseList);
  }

  var model = BadEventModel();

  var currentIndex = 0;

  Future<void> getListInfo() async {
    showLoadingState();
    try{
      groupList =  await model.serviceGroupList();
      if(groupList.isNotEmpty){
        showContent();
      }else{
        showEmptyState();
      }
    }catch(e){
      showErrorState(e.toNetError());
    }
  }

  void clickEmployee(dynamic employee){
    var list = selectedList.where((element) => element['humanId']==employee['humanId']);
    if(list.isEmpty){
      selectedList.add({"name":employee['userName'],"humanId":employee['humanId']});
    }else{
      selectedList.removeWhere((element) => element['humanId']==employee['humanId']);
    }
    notifyListeners();
  }

  bool isContain(dynamic employee){
    var list = selectedList.where((element) => element['humanId']==employee['humanId']);
    return list.isNotEmpty;
  }


  @override
  void onCreated() {
    super.onCreated();
    getListInfo();
  }
}